<template>
  <el-dropdown trigger="click">
    <!-- <div class="avatar">
      <img src="@/assets/images/avatar.gif" alt="avatar" />
    </div> -->
    <span class="avatar-dropdown">
      <el-avatar class="user-avatar" :src="getImgUrl(avatar)" :alt="username" />
      <div class="user-name">
        <span class="hidden-xs-only" :title="username">{{ username }}</span>
        <el-icon><ArrowDown /></el-icon>
      </div>
    </span>
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item @click="openDialog('infoRef')">
          <el-icon><User /></el-icon>{{ $t("header.personalData") }}
        </el-dropdown-item>
        <!-- <el-dropdown-item @click="openDialog('passwordRef')">
          <el-icon><Edit /></el-icon>{{ $t("header.changePassword") }}
        </el-dropdown-item> -->
        <el-dropdown-item @click="logout" divided>
          <el-icon><SwitchButton /></el-icon>{{ $t("header.logout") }}
        </el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>
  <!-- infoDialog -->
  <InfoDialog ref="infoRef"></InfoDialog>
  <!-- passwordDialog -->
  <PasswordDialog ref="passwordRef"></PasswordDialog>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { LOGIN_URL } from "@/config";
import { useRouter } from "vue-router";
import { logoutApi } from "@/api/modules/login";
import { useUserStore } from "@/stores/modules/user";
import { ElMessageBox, ElMessage } from "element-plus";
import InfoDialog from "./InfoDialog.vue";
import PasswordDialog from "./PasswordDialog.vue";
import { computed } from "vue";
import { getImgUrl } from "@/api/mains";

const router = useRouter();
const userStore = useUserStore();

const username = computed(() => userStore.userInfo.username);
const avatar = computed(() => userStore.userInfo.avatar);

// 退出登录
const logout = () => {
  ElMessageBox.confirm("您是否确认退出登录?", "温馨提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(async () => {
    // 1.执行退出登录接口
    await logoutApi();

    // 2.清除 Token
    userStore.setToken("");

    // 3.重定向到登陆页
    router.replace(LOGIN_URL);
    ElMessage.success("退出登录成功！");
  });
};

// 打开修改密码和个人信息弹窗
const infoRef = ref<InstanceType<typeof InfoDialog> | null>(null);
const passwordRef = ref<InstanceType<typeof PasswordDialog> | null>(null);

const openDialog = async (ref: string) => {
  //if (ref == "infoRef") infoRef.value?.openDialog();
  //if (ref == "passwordRef") passwordRef.value?.openDialog();

  if (ref) {
    await router.push("/home/personalList");
  }
};
</script>

<style scoped lang="scss">
.avatar {
  width: 40px;
  height: 40px;
  overflow: hidden;
  cursor: pointer;
  border-radius: 50%;
  img {
    width: 100%;
    height: 100%;
  }
}
.avatar-dropdown {
  display: flex;
  place-content: center center;
  place-items: center center;
  .user-avatar {
    flex-shrink: 0;
    width: 28px;
    height: 28px;
    margin-left: 15px;
    cursor: pointer;
    border-radius: 50%;
  }
  .user-name {
    position: relative;
    display: flex;
    flex-shrink: 0;
    align-content: center;
    align-items: center;
    height: 40px;
    margin-left: 6px;
    line-height: 40px;
    cursor: pointer;
    span {
      max-width: 100px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    [class*="ri-"] {
      margin-left: 3px !important;
    }
  }
}
</style>
